﻿using AutoMapper.Internal.Mappers;
using Himp.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊导诊记录
    /// </summary>
    public class OtpQueueRecService : CrudAppService<OtpQueueRec, OtpQueueRecDto, Guid, OtpQueueRecInput, OtpQueueRecCreateDto, OtpQueueRecUpdateDto>, IOtpQueueRecService
    {
        private IRepository<OtpQueueRec, Guid> _optSchdTypeRepository;
        private IOtpRgstRecService _otpRgstRecService;

        public OtpQueueRecService(IRepository<OtpQueueRec, Guid> optSchdTypeRepository
            , IOtpRgstRecService otpRgstRecService) : base(optSchdTypeRepository)
        {
            _optSchdTypeRepository = optSchdTypeRepository;
            _otpRgstRecService = otpRgstRecService;
        }

        /// <summary>
        /// 获取门诊导诊记录列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<OtpQueueRecDto>> GetListAsync(OtpQueueRecInput input)
        {
            var iQuery = await _optSchdTypeRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.DeptCodg), s => s.DeptCodg == input.DeptCodg);
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.OperRmCodg), s => s.OperRmCodg == input.OperRmCodg);
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.DrCodg), s => s.DrCodg == input.DrCodg);
            iQuery = iQuery.WhereIf(input.BegnDate != null, s => s.BegnDate.Date == input.BegnDate);
            iQuery = iQuery.WhereIf(input.OtpNavStatus != null, d => d.OtpNavStatus == (EnumOtpNavStatus)input.OtpNavStatus);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpQueueRec>, List<OtpQueueRecDto>>(iQuery.ToList());

                return new PagedResultDto<OtpQueueRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpQueueRec>, List<OtpQueueRecDto>>(items.ToList());

                return new PagedResultDto<OtpQueueRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 分诊
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async override Task<OtpQueueRecDto> CreateAsync(OtpQueueRecCreateDto input)
        {
            var otpRgstRec = await _otpRgstRecService.GetAsync(input.OtpRgstRecId);
            OtpQueueRec otpNavRec = new OtpQueueRec();
            otpNavRec.OtpNavType = input.OtpNavType;
            otpNavRec.PatnId = otpRgstRec.PatnId;
            otpNavRec.MdtrtSn = otpRgstRec.MdtrtSn;
            otpNavRec.PatnTypeName = otpRgstRec.SetlTypeName;
            otpNavRec.OtpNo = otpRgstRec.OtpNo;
            otpNavRec.PatnName = otpRgstRec.PatnName;
            otpNavRec.Age = otpRgstRec.Age;
            otpNavRec.Gender = (EnumGender)otpRgstRec.Gender;
            otpNavRec.OtpNavNo = Guid.NewGuid().ToString();
            otpNavRec.DeptCodg = otpRgstRec.DeptCodg;
            otpNavRec.DeptName = otpRgstRec.DeptName;
            otpNavRec.OperRmCodg = input.OperRmCodg; 
            otpNavRec.OperRmName = input.OperRmName;
            otpNavRec.DrCodg = otpRgstRec.DrCodg;
            otpNavRec.DrName = otpRgstRec.DrName;
            otpNavRec.BegnDate = otpRgstRec.RgstDate;
            otpNavRec.SignInTime = DateTime.Now;
            otpNavRec.OtpNo = otpRgstRec.OtpNo;
            otpNavRec.OtpNavStatus = EnumOtpNavStatus.已登记;
            otpNavRec = await _optSchdTypeRepository.InsertAsync(otpNavRec);
            var itemDto = ObjectMapper.Map<OtpQueueRec, OtpQueueRecDto>(otpNavRec);

            return itemDto;
        }

        ///// <summary>
        ///// 签到
        ///// </summary>
        ///// <param name="otpRgstRec"></param>
        ///// <returns></returns>
        //public async Task<OtpNavRecDto> PostAsync(Guid otpRgstRecId)
        //{

        //    return itemDto;
        //}
    }
}
